# pdf 转 txt 文本文件

import importlib
import sys
import time
 
importlib.reload(sys)
time1 = time.time()
 
import os.path
from pdfminer.pdfparser import  PDFParser,PDFDocument
from pdfminer.pdfinterp import PDFResourceManager, PDFPageInterpreter
from pdfminer.converter import PDFPageAggregator
from pdfminer.layout import LTTextBoxHorizontal,LAParams
from pdfminer.pdfinterp import PDFTextExtractionNotAllowed

def clearStr(strList):
    rList = []
    lastLineNeedBreak = False # 下一行是否需要换行
    breakStr = ''
    for s in strList:
        s = s.strip()
        # s = s.replace(' ','').replace('\n','').replace('\r','')
        if len(s) == 0:
            # print('清除空白行---------')
            continue
        # if '这些概念是如何相' in s:
        #     print('找到啦：'+s)
        #     for ss in s:
        #         if ss in ['\n','\r','\t']:
        #             print('有空白')
        #         else:
        #             print(ss)
        if lastLineNeedBreak or s[0:] == '•':
            # print('====换行了：'+s+'=======>上一行'+breakStr)
            rList.append(s)
        else:
            rListLen = len(rList)
            if rListLen == 0:
                rList.append(s)
            else:
                rList[rListLen - 1] += s
        lastChart = s[-1]
        if lastChart in ["。", "；"]:
            # 下一行是否 ===》需要换行
            lastLineNeedBreak = True
            breakStr = s
        else:
            # 不需要换行
            if s[-1] == '\n':
                s = s[:-1]
            if s[0] == '\n':
                s = s[:1]
            lastLineNeedBreak = False
    rStr = '\n'.join(map(str, rList))
    return rStr
def parse(pdf_path,txt_path):
    '''解析PDF文本，并保存到TXT文件中'''
    fp = open(pdf_path,'rb')
    # pdf1 = urlopen('http://www.tencent.com/20160321.pdf')
    #用文件对象创建一个PDF文档分析器
    parser = PDFParser(fp)
    #创建一个PDF文档
    doc = PDFDocument()
    #连接分析器，与文档对象
    parser.set_document(doc)
    doc.set_parser(parser)
 
    #提供初始化密码，如果没有密码，就创建一个空的字符串
    doc.initialize()
 
    #检测文档是否提供txt转换，不提供就忽略
    if not doc.is_extractable:
        raise PDFTextExtractionNotAllowed
    else:
        #创建PDF，资源管理器，来共享资源
        rsrcmgr = PDFResourceManager()
        #创建一个PDF设备对象
        laparams = LAParams()
        device = PDFPageAggregator(rsrcmgr,laparams=laparams)
        #创建一个PDF解释其对象
        interpreter = PDFPageInterpreter(rsrcmgr,device)
 
        #循环遍历列表，每次处理一个page内容
        # doc.get_pages() 获取page列表
        pageCtn = 0
        f = open(txt_path, 'w', encoding="utf-8")
        allList = []
        for page in doc.get_pages():
            # if pageCtn > 20:
            #     break
            pageCtn += 1
            interpreter.process_page(page)
            #接受该页面的LTPage对象
            layout = device.get_result()
            # 这里layout是一个LTPage对象 里面存放着 这个page解析出的各种对象
            # 一般包括LTTextBox, LTFigure, LTImage, LTTextBoxHorizontal 等等
            # 想要获取文本就获得对象的text属性，
            strList = []
            for x in layout:
                if(isinstance(x,LTTextBoxHorizontal)):
                        # 字符块
                        results = x.get_text().replace("\x0c", '').replace(' ', '')
                        rStr = clearStr(results.split('\n'))
                        if rStr != '谢谢':
                            strList.append(rStr)
            allList.append(clearStr(strList))
            print('page '+str(pageCtn)+' done')
        rStr = clearStr(allList)
        print(rStr)
        f.write(rStr)
        f.close()
def parsePDF():
    parse(pdf_path,txt_path)
    time2 = time.time()
    print("总共消耗时间为:",time2-time1)
def parseMD():
    mdPath = './1.md'
    # 打开 Markdown 文件并读取内容
    with open(mdPath, 'r',encoding="utf-8") as file:
        content = file.read()
    # 在每个段落后追加换行符
    list = content.split('\n')
    content = '\n\n'.join(map(str,list))
    print(content)
    # 打开 Markdown 文件并写入修改后的内容
    with open(mdPath, 'w',encoding="utf-8") as file:
        file.write(content)
    file.close()
pdf_path = './test.pdf'
txt_path = './test.txt'
if __name__ == '__main__':
    # parsePDF()
    parseMD()